<?php
class ObjRequest
{
	public $Table="tblrequest";
	public $RequestID;
	public $Title;
	public $Detail;
	public $ResponseBy;
	public $ResponseDate;
	public $ResolveBy;
	public $ResolveDate;
	public $Status;
	public $LastUpdate;
	
	function loadByKey($id)
	{
		$ob=new ClassConnect();
		$sql="select * from `".$this->Table."` where RequestID='".$id."'";
		$row=$ob->singleData($sql);
		$this->RequestID=$row['RequestID'];
		$this->Title=$row['Title'];
		$this->Detail=$row['Detail'];
		$this->ResponseBy=$row['ResponseBy'];
		$this->ResponseDate=$row['ResponseDate'];
		$this->ResolveBy=$row['ResolveBy'];
		$this->ResolveDate=$row['ResolveDate'];
		$this->Status=$row['Status'];
		$this->LastUpdate=$row['LastUpdate'];
	}
	function getTable($dk)
	{
		$ob=new ClassConnect();
		$sql="select * from `".$this->Table."` $dk";
		$data=$ob->mutiData($sql);
		return $data;
	}
	function travel($dk)
	{
		$arr=array();
		$data=$this->getTable($dk);
		for($i=0;$i<count($data);$i++)
			array_push($arr,$data[$i]['RequestID']);
		return $arr;
	}
	function insert($conn)
	{
		$ob=new ClassConnect();
		$dt=new ClassDateTime();
		$this->RequestID=$ob->getNextIdVarChar($this->Table,"RequestID",$dt->_getYearhNow().$dt->_getMonthNow().$dt->_getDayNow());
		
		$this->LastUpdate=$dt->_getNow("-");
		$field=array(
						"RequestID",
						"Title",
						"Detail",
						"ResponseBy",
						"ResponseDate",
						"ResolveBy",
						"ResolveDate",
						"Status",
						"LastUpdate"
					);
		$value=array(
						$this->RequestID,
						$this->Title,
						$this->Detail,
						$this->ResponseBy,
						$this->ResponseDate,
						$this->ResolveBy,
						$this->ResolveDate,
						$this->Status,
						$this->LastUpdate
					);
		return $ob->insertData($conn,$this->Table,$field,$value);
	}
	function update($conn)
	{
		$ob=new ClassConnect();
		$dt=new ClassDateTime();
		$this->LastUpdate=$dt->_getNow("-");
		$field=array(
						"RequestID",
						"Title",
						"Detail",
						"ResponseBy",
						"ResponseDate",
						"ResolveBy",
						"ResolveDate",
						"Status",
						"LastUpdate"
					);
		$value=array(
						$this->RequestID,
						$this->Title,
						$this->Detail,
						$this->ResponseBy,
						$this->ResponseDate,
						$this->ResolveBy,
						$this->ResolveDate,
						$this->Status,
						$this->LastUpdate
					);
		$where="RequestID = '".$this->RequestID."'";
		
		return $ob->updateData($conn,$this->Table,$field,$value,$where);			
	}
	function delete($conn)
	{
		$ob=new ClassConnect();		
		$where="RequestID = '".$this->RequestID."'";
		return $ob->deleteData($conn,$this->Table,$where);
			
	}
	function insertAttachment($conn,$FileID)
	{
		$ob=new ClassConnect();
		$table="tblrequest_file";
		$field=array("RequestID","FileID");
		$value=array($this->RequestID,$FileID);
		$ob->insertData($conn,$table,$field,$value);
	}
	function getAttachment()
	{
		$ob=new ClassConnect();
		$sql="select * from `tblrequest_file` WHERE RequestID='".$this->RequestID."' ORDER BY FileID";
		$data=$ob->mutiData($sql);
		return $data;
	}
	function listStatus()
	{
		$arr=array(
					"0" => array("Name"=> "Wait for reponse","Color"=>"#fbb5b5"),
					"1" => array("Name"=> "Pending","Color"=>"#fcfcca"),
					"2" => array("Name"=> "Complete","Color"=>"#e9fec9"),
					"3" => array("Name"=> "Close","Color"=>"#a1a1a1"),
					);
		return $arr;
	}
	function getStatus()
	{
		$arr=$this->listStatus();
		return $arr[$this->Status];
	}
	function cbStatus($name,$selectValue,$action)
	{

		$arr=$this->listStatus();
		$cb="<select name='$name' onChange=\"$action\">";
		foreach($arr as $key => $val)
		{
			$sel="";
			if($key == $selectValue)
				$sel="selected";
			$cb.="<option value='".$key."' $sel>".$val['Name']."</option>";
		}
		$cb.="</select>";
		return $cb;
	}
	function updateStatus($conn)
	{
		$ob=new ClassConnect();
		$field=array("RequestID","Status");
		$value=array($this->RequestID,$this->Status);
		$where="RequestID = '".$this->RequestID."'";
		
		$ob->updateData($conn,$this->Table,$field,$value,$where);
	}
	function updateResolve($conn)
	{
		$ob=new ClassConnect();
		$field=array("RequestID","ResolveBy","ResolveDate");
		$value=array($this->RequestID,$this->ResolveBy,$this->ResolveDate);
		$where="RequestID = '".$this->RequestID."'";
		$ob->updateData($conn,$this->Table,$field,$value,$where);
	}
	function getPost()
	{
		$obj= new ObjPost();
		$arr=$obj->travel( " WHERE RequestID = '". $this->RequestID. "' ORDER BY PostID" );
		return $arr;
	}
}

class ObjFile
{
	public $Table="tblfile";
	public $FileID;
	public $FileName;
	function loadByKey($id)
	{
		$ob=new ClassConnect();
		$sql="select * from `".$this->Table."` where FileID='".$id."'";
		$row=$ob->singleData($sql);
		$this->FileID=$row['FileID'];
		$this->FileName=$row['FileName'];
	}
	function getTable($dk)
	{
		$ob=new ClassConnect();
		$sql="select * from `".$this->Table."` $dk";
		$data=$ob->mutiData($sql);
		return $data;
	}
	function travel($dk)
	{
		$arr=array();
		$data=$this->getTable($dk);
		for($i=0;$i<count($data);$i++)
			array_push($arr,$data[$i]['FileID']);
		return $arr;
	}
	function insert($conn,$inputName)
	{
		$ob=new ClassConnect();
		$this->FileID=$ob->getNextId($this->Table,"FileID");
		
		$targetbr = "modules/".$this->Table."/";
		if (! is_dir($targetbr))
			mkdir( $targetbr , 0777 );
		$obupload=new Upload();
		$uploadDir=$targetbr;
		$outputName="file".$this->FileID;
		$out = $obupload->uploadFile($inputName, $uploadDir,$outputName);
		$this->FileName=$out;
		
		$field=array("FileID","FileName");
		$value=array($this->FileID,$this->FileName);
		return $ob->insertData($conn,$this->Table,$field,$value);
	}
	function update($conn)
	{
		$ob=new ClassConnect();
		
		$obj = new Manufacturer();
		
		$obj->loadByKey($this->FileID);
		$image=basename( $_FILES['image']['name']);
		if($image!="")
		{
			$targetbr = "modules/".$this->Table."/";
			if (! is_dir($targetbr))
				mkdir( $targetbr , 0777 );
			$obupload=new Upload();
			$inputName="image";
			$uploadDir=$targetbr;
			$outputName=$this->Table.$this->FileID;
			$out=$obupload->uploadFile($inputName, $uploadDir,$outputName);
			$this->FileName=$out;
		}
		else
			$this->FileName=$obj->Image;
			
		$field=array("FileID","FileName");
		$value=array($this->FileID,$this->FileName);
		$where="FileID = '".$this->FileID."'";
		
		return $ob->updateData($conn,$this->Table,$field,$value,$where);			
	}
	function delete($conn)
	{
		$ob=new ClassConnect();		
		$this->loadByKey($this->FileID);
		
		$targetbr = "modules/".$this->Table."/";
		$myFile = $targetbr.$this->Image;
		unlink($myFile);
		
		$where="FileID = '".$this->FileID."'";
		return $ob->deleteData($conn,$this->Table,$where);
			
	}
}

class ObjPost
{
	public $Table="tblpost";
	public $PostID;
	public $RequestID;
	public $PostBy;
	public $Detail;
	public $LastUpdate;
	
	function loadByKey($id)
	{
		$ob=new ClassConnect();
		$sql="select * from `".$this->Table."` where PostID='".$id."'";
		$row=$ob->singleData($sql);
		$this->PostID=$row['PostID'];
		$this->PostBy=$row['RequestID'];
		$this->PostBy=$row['PostBy'];
		$this->Detail=$row['Detail'];
		$this->LastUpdate=$row['LastUpdate'];
	}
	function getTable($dk)
	{
		$ob=new ClassConnect();
		$sql="select * from `".$this->Table."` $dk";
		$data=$ob->mutiData($sql);
		return $data;
	}
	function travel($dk)
	{
		$arr=array();
		$data=$this->getTable($dk);
		for($i=0;$i<count($data);$i++)
			array_push($arr,$data[$i]['PostID']);
		return $arr;
	}
	function insert($conn)
	{
		$ob=new ClassConnect();
		$dt=new ClassDateTime();
		$this->PostID = $ob->getNextId($this->Table,"PostID");
		
		$this->LastUpdate=$dt->_getNow("-");
		$field=array(
						"PostID",
						"RequestID",
						"PostBy",
						"Detail",
						"LastUpdate"
					);
		$value=array(
						$this->PostID,
						$this->RequestID,
						$this->PostBy,
						$this->Detail,
						$this->LastUpdate
					);
		return $ob->insertData($conn,$this->Table,$field,$value);
	}
	function update($conn)
	{
		$ob=new ClassConnect();
		$dt=new ClassDateTime();
		$this->LastUpdate=$dt->_getNow("-");
		$field=array(
						"PostID",
						"RequestID",
						"PostBy",
						"Detail",
						"LastUpdate"
					);
		$value=array(
						$this->PostID,
						$this->RequestID,
						$this->PostBy,
						$this->Detail,
						$this->LastUpdate
					);
		$where="PostID = '".$this->PostID."'";
		return $ob->updateData($conn,$this->Table,$field,$value,$where);			
	}
	function delete($conn)
	{
		$ob=new ClassConnect();		
		$where="PostID = '".$this->PostID."'";
		return $ob->deleteData($conn,$this->Table,$where);
			
	}
	function insertAttachment($conn,$FileID)
	{
		$ob=new ClassConnect();
		$table="tblpost_file";
		$field=array("PostID","FileID");
		$value=array($this->PostID,$FileID);
		$ob->insertData($conn,$table,$field,$value);
	}
	function getAttachment()
	{
		$ob=new ClassConnect();
		$sql="select * from `tblpost_file` WHERE PostID='".$this->PostID."' ORDER BY FileID";
		$data=$ob->mutiData($sql);
		return $data;
	}
}

?>
